package com.zhz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.SysUser;
import com.zhz.model.R;
import com.zhz.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/25
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 员工管理
 **/
@Api(tags = "员工管理")
@RequestMapping("/users")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping()
    @ApiOperation(value = "查询员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="current",value = "当前页"),
            @ApiImplicitParam(name="size",value = "每页显示的条数"),
            @ApiImplicitParam(name="mobile",value = "员工的手机号码"),
            @ApiImplicitParam(name="fullname",value = "员工的全名称")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> findByPage(@ApiIgnore Page<SysUser> page,String mobile,String fullname){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> pageData= sysUserService.findByPage(page,mobile,fullname);
        return R.ok(pageData);
    }
    @PostMapping()
    @ApiOperation(value = "新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sysUser",value = "sysUser的json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R addUser(@RequestBody SysUser sysUser){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
       sysUser.setCreateBy(userId);
        return sysUserService.addUser(sysUser)?R.ok():R.fail("新增失败") ;
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R deleteUser( @RequestBody  Long ids[] ){
        return sysUserService.removeByIds(Arrays.asList(ids))?R.ok():R.fail();
    }
}
